<?php

global $pa;

function index()
{
	global $couleur; 
	
	$color = $couleur['16'];
	$bg = $couleur['17'];
	$border = $couleur['15'];

	$sql = "SELECT `id`, `icone`, `poste`, `places`, `details`, `modele` FROM `".TBL_RECRUTEMENTS."` WHERE activer = 'oui'";
	$req = mysql_query($sql);
	$nb = mysql_num_rows($req);
	$i = 1;
	while(list($id, $icone, $poste, $places, $details, $modele, $activer) = mysql_fetch_array($req))
	{
		if(($color == $couleur['16'])&&($bg == $couleur['17'])&&($border == $couleur['15'])) 
		{
			$color = $couleur['13']; $bg = $couleur['14']; $border = $couleur['18'];
		}
		else 
		{
			$color = $couleur['16']; $bg = $couleur['17']; $border = $couleur['15'];
		}
		
		$paql2 = "SELECT `titre` FROM `".TBL_R_MODELES."` WHERE id='".$modele."'";
		$req2 = mysql_query($paql2);
		list($titre) = mysql_fetch_array($req2);
		
		$contenu = "<table style=\"width: 100%;\">\n";
		$contenu .= "<tr style=\"border-bottom: 1px solid ".$border.";\">
		<td style=\"padding: 0 5px 5px 0; width: 24px;\"><img src=\"".RECRUTEMENTS.$icone."\" style=\"width: 24px; height: 24px;\"></td>
		<td style=\"width: 50%; text-align: left; padding: 0 0 5px 0;\"><a name=\"".$poste."\">(".$places.")&nbsp;".$poste."</a></td>
		<td style=\"width: 50%; padding: 0 0 5px 0; text-align: right\"><b><a href=\"index.php?do=recrutements&pa=demande&id=".$id."\">POSTULER !</a></b></td>
		</tr>
		<tr>
		<td style=\"padding: 5px;\" colspan=\"3\">".nl2br($details)."</td>
		</tr>\n";
	$contenu .= "</table>\n";
	
	$chaine = new arrondis();
	$chaine->texte = $contenu;
	$chaine->coin_superieur_droit = "0";
	$chaine->coin_inferieur_gauche = "0";
	$chaine->bordure = $border;
	$chaine->fond = $bg;
	$chaine->ecriture = $color;
	echo $chaine->construire();
	if($i != $nb) {echo "<br/>";}
	$i++;
	}
}

function demande()
{
	global $formulaire;
	
	function text_replace($i)
	{
		global $formulaire;
		
		if (preg_match("{TEXT,".$i."}", $formulaire)) 
		{
			$formulaire = str_replace("{TEXT,".$i."}", "<input type=\"text\" name=\"text_".$i."\" id=\"text_".$i."\" />", $formulaire);
		}
		
		$i++;
		
		if (preg_match("{TEXT,".$i."}", $formulaire)) 
		{
			text_replace($i);
		}
		
		return $formulaire;
	}	
	
	function textarea_replace($i)
	{
		global $formulaire;
		
		if (preg_match("{TEXTAREA,".$i."}", $formulaire)) 
		{
			$formulaire = str_replace("{TEXTAREA,".$i."}", "<textarea name=\"text_".$i."\" id=\"text_".$i."\" cols=\"40\" rows=\"10\"></textarea>", $formulaire);
		}
		
		$i++;
		
		if (preg_match("{TEXTAREA,".$i."}", $formulaire)) 
		{
			textarea_replace($i);
		}
		
		return $formulaire;
	}
	
	function liste_replace($i)
	{
		global $formulaire;
		
		if (preg_match("{LISTE,".$i."}", $formulaire)) 
		{
			$formulaire = str_replace("{LISTE,".$i."}", "<select name=\"text_".$i."\" id=\"text_".$i."\">", $formulaire);
			$formulaire = preg_replace("/\{OPTION,".$i."\}(.*?)\{\/OPTION\}/i", "<option>\\1</option>", $formulaire);
			$formulaire = str_replace("{/LISTE}", "</select>", $formulaire);
		}
		
		$i++;
		
		if (preg_match("{LISTE,".$i."}", $formulaire)) 
		{
			liste_replace($i);
		}
		
		return $formulaire;
	}
	
	$id = $_GET['id'];
	
	$paql = "SELECT `id`, `icone`, `poste`, `places`, `details`, `modele`, `activer` FROM `".TBL_RECRUTEMENTS."` WHERE id='".$id."' AND activer='oui'";
	$req = mysql_query($paql);
	list($id, $icone, $poste, $places, $details, $modele, $activer) = mysql_fetch_array($req);
	
	$paql2 = "SELECT `titre`, `formulaire` FROM `".TBL_R_MODELES."` WHERE id='".$modele."'";
	$req2 = mysql_query($paql2);
	list($titre, $formulaire) = mysql_fetch_array($req2);

	$formulaire = "(tableau)
	(ligne)
	(colonne style=width: 34%; border-right: 1px solid black; padding: 5px;)Pseudo(/colonne)
	(colonne style=width: 66%; padding: 5px)(text name=\"1\")(/colonne)
	(/ligne)
	(ligne)
	(colonne fusion_h=2 style=text-align: right;)
	(envoi)Envoyer(/envoi)
	(/colonne)
	(/ligne)
	(/tableau)";
	
	$formulaire = bbcode($formulaire);
	
	echo "<table style=\"width: 100%;\">
	<tr>
	<td style=\"width: 100%; padding: 5px;\">Vous allez postuler pour le poste de: ".$poste." (".$places.")</td>
	</tr>
	<tr>
	<td style=\"width: 100%; padding: 5px;\">".$formulaire."</td>
	</tr>
	</table>";
	
}

switch ($pa)
{
    case "index": index(); break;
    case "demande": demande(); break;
}

?>